package cao.oschina.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import hd.util.ScreenHelper;

/**
 * 刷新视图（两个圆，下拉时候两个大圆，开始刷新时候逐渐变小并且开始旋转）
 * <p/>
 * Created by Haidy on 15/3/13.
 */
public class RefreshView extends View {

    private final int MAX_RADIUS;   //最大半径
    private final int OFFSET_RADIUS;//离中心的半径
    private final float offsetRadius;

    private int degrees = 0;        //旋转的角度
    private float firstRadius;      //第一个圆的半径
    private float secondRadius;

    private boolean rotation = false;

    private Paint paint;

    public RefreshView(Context context) {
        this(context, null);
    }

    public RefreshView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RefreshView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        MAX_RADIUS = ScreenHelper.dp2px(context, 3);
        OFFSET_RADIUS = ScreenHelper.dp2px(context, 5);
        offsetRadius = OFFSET_RADIUS * 3 / (float) 180;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        firstRadius = secondRadius = MAX_RADIUS;
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
    }

    /**
     * 重置
     */
    public void reset() {
        degrees = 0;
        firstRadius = secondRadius = MAX_RADIUS;
        invalidate();
    }

    /**
     * 开始旋转
     */
    public void startRotation() {
        rotation = true;
        reset();
    }

    /**
     * 停止旋转
     */
    public void stopRotation() {
        rotation = false;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(getWidth() / 2, getHeight() / 2);
        if (rotation) {
            canvas.rotate(degrees);
            degrees += 3;
            if (degrees >= 359) {
                degrees = 0;
            }
            if (degrees < 180) {
                firstRadius -= offsetRadius;
                if (secondRadius < MAX_RADIUS) {
                    secondRadius += offsetRadius;
                }
            } else {
                secondRadius -= offsetRadius;
                if (firstRadius < MAX_RADIUS) {
                    firstRadius += offsetRadius;
                }
            }
            invalidate();
        } else {
            if (degrees != 0) {
                canvas.rotate(degrees);
                degrees += 10;
                if (degrees > 359) {
                    degrees = 0;
                }
            }
            if (firstRadius < MAX_RADIUS) {
                firstRadius += offsetRadius * 3;
            }
            if (secondRadius < MAX_RADIUS) {
                secondRadius += offsetRadius * 3;
            }
            if (degrees != 0 || firstRadius < MAX_RADIUS || secondRadius < MAX_RADIUS) {
                invalidate();
            } else {
                degrees = 0;
                firstRadius = secondRadius = MAX_RADIUS;
            }
        }
        canvas.drawCircle(0, OFFSET_RADIUS, firstRadius, paint);
        canvas.drawCircle(0, -OFFSET_RADIUS, secondRadius, paint);
        canvas.restore();
    }
}
